package edu.whu.MagicNote.controller;

import edu.whu.MagicNote.constant.MessageConstant;
import edu.whu.MagicNote.domain.Result;
import edu.whu.MagicNote.domain.dto.TemplateDTO;
import edu.whu.MagicNote.domain.vo.TemplateVO;
import edu.whu.MagicNote.service.ITemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/template")
@Slf4j
public class TemplateController {
    @Autowired
    private ITemplateService templateService;

    @GetMapping("/getall/{userId}")
    public Result getAll(@PathVariable Long userId) {
        TemplateDTO templateDTO = new TemplateDTO();
        templateDTO.setUserId(userId);
        List<TemplateVO> templates = templateService.getTemplateVOs(templateDTO);
        return Result.builder().data(templates).build();
    }

    @PostMapping("/star")
    public Result star(@RequestBody TemplateDTO templateDTO) {
        templateService.star(templateDTO);
        return Result.builder()
                .msg(MessageConstant.STAR_SUCCESS)
                .build();
    }

    @DeleteMapping("/unstar")
    public Result unstar(TemplateDTO templateDTO) {
        templateService.unstar(templateDTO);
        return Result.builder()
                .msg(MessageConstant.UNSTAR_SUCCESS)
                .build();
    }

    @GetMapping("/getByType")
    public Result getByType(TemplateDTO templateDTO) {
        List<TemplateVO> templates = templateService.getTemplateVOs(templateDTO);
        return Result.builder()
                .data(templates)
                .build();
    }
}
